package com.ten.jinxiancun.controller;


import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import com.ten.jinxiancun.entity.User;
import com.ten.jinxiancun.server.impl.logServerImpl;
import com.ten.jinxiancun.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/public")
public class logController {

    @Autowired
    logServerImpl lsl;

    /**
     * @param u
     * @return
     */
    @RequestMapping("/login")
    public JSONObject login(User u) {
        JSONObject jsonObject = new JSONObject();
        User UserForBase = lsl.logUserFind(u.getName());
        Integer errorCount = 0;// 判断登录出错的次数
        UsernamePasswordToken t = null;
        if (UserForBase == null) {
            jsonObject.put("success", false);
            jsonObject.put("message", "登录失败,用户不存在");
            return jsonObject;
        } else {
            if (!MD5Util.verify(u.getPassword(), UserForBase.getPassword())) {
                // 密码不对,修改密码错误 +1
                jsonObject.put("success", false);
                String msg;
                if (errorCount == 5) {
                    msg = "密码错误次数上线,账户已经锁定!<br/>请找回密码或者联系管理员重置密码.";
                } else {
                    msg = "密码错误!你还有" + (5 - errorCount - 1) + "次机会";
                }
                jsonObject.put("message", msg);
                return jsonObject;
            } else if (UserForBase.getCrtId() == 1) {
                jsonObject.put("success", false);
                jsonObject.put("message", "登录失败,账户已锁定,账户已经锁定!<br/>请找回密码或者联系管理员重置密码");
                return jsonObject;
            } else {
                // 登录成功把错误次数改成0
                try {
                    Subject subject = SecurityUtils.getSubject();
                    t = new UsernamePasswordToken(UserForBase.getName(), UserForBase.getPassword());
                    subject.login(t);
                } catch (AuthenticationException e) {
                    // TODO: handle exception
                    e.printStackTrace();
                } catch (AuthorizationException e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
        }
        jsonObject.put("success", true);
        jsonObject.put("token", t);
        jsonObject.put("user", UserForBase);
        return jsonObject;
    }
 /*   @RequestMapping("/update")
    @ResponseBody
    public Object updateIs(@RequestParam Map<String,Object>map) {
        int i= us.findUpdate(map);
        Result res=new Result("0","true",i,true);
        return res;
    }*/


//
//	@RequestMapping("updatePassword")
//	public Object updatePassword(@RequestParam Map<String, Object> map) {
//		System.out.println("===>"+map);
//		String old= map.get("old_password").toString();
//		Users u = us.getByUserName(map.get("username").toString());
//		Result res=null;
//		if (!MD5Util.verify(old,u.getPassword())) {
//			// 密码不对,修改密码错误次数+1
//			String msg;
//			if (u.getPsdWrongTime() == 5) {
//				us.IsLock(u.getId(), 1);
//				msg = "旧密码错误次数上限,账户以锁定!<br/>请找回密码或联系管理员重置密码。";
//			} else {
//				us.errorCount(u.getId(),u.getErrorCount()+1);
//				msg = "旧密码错误! 你还有" + (5 - u.getErrorCount() - 1) + "次机会!";
//			}
//			res=new Result("1", msg, -1, false);
//		} else if (map.get("new_password").equals(map.get("again_password"))==false) {
//			res=new Result("1", "两次输入密码不一致", -1, false);
//		}else {
//			map.put("newPassword", MD5Util.getMD5(map.get("new_password").toString()));
//			us.errorCount(u.getId(),0);
//			res=new Result("0", "密码修改成功",us.findUpdateUsers(map), true);
//		}
//		return res;
//
//
//	}

}
